package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianfeng.lingshijia.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import custom.ShoppingItem_footView;
import custom.ShoppingItem_headView;
import utils.DownloadUtils;
import utils.ShoppingCart;
import utils.ShoppingGroup;

public class ExpandableAdapter extends BaseExpandableListAdapter {
    List<ShoppingGroup> parent_data;
    Map<String, List<ShoppingCart>> child;
    Context context;
    OnselectChangeListenner selectChangeListenner;

    public ExpandableAdapter(Context context) {
        parent_data = new ArrayList<>();
        child = new HashMap<>();
        this.context = context;
    }

    public void setParent(List<ShoppingGroup> parent) {
        this.parent_data = parent;
        notifyDataSetChanged();
    }

    public void setChild(Map<String, List<ShoppingCart>> child) {
        this.child = child;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return parent_data.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return child.get(parent_data.get(groupPosition).getStoreName()).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return parent_data.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child.get(parent_data.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
        ShoppingItem_headView view = new ShoppingItem_headView(context);
        view.setData(parent_data.get(groupPosition).getStoreName());
        view.check.setChecked(parent_data.get(groupPosition).isChecked());
        view.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                parent_data.get(groupPosition).setIsChecked(isChecked);
                selectChangeListenner.groupChecked(groupPosition,isChecked);
                selectChangeListenner.calculateCost();
            }
        });
        return view;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        String key = parent_data.get(groupPosition).getStoreName();
        final ShoppingCart cart = child.get(key).get(childPosition);

        if (isLastChild) {
            return new ShoppingItem_footView(context);
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.shoppingitem_lv_layout, null);

            ImageView goodsPic = (ImageView) convertView.findViewById(R.id.shopping_lv_pic);
            TextView title = (TextView) convertView.findViewById(R.id.shopping_lv_title);
            TextView price = (TextView) convertView.findViewById(R.id.shopping_price);

            if (cart != null) {
                DownloadUtils.LoadPicture(cart.getPic_url(), goodsPic);
                title.setText(cart.getProductName());
                price.setText("¥" + cart.getPrice());
            }
            ImageView add = (ImageView) convertView.findViewById(R.id.shopping_add);
            ImageView reduce = (ImageView) convertView.findViewById(R.id.shopping_lv_reduce);
            final TextView shoppingcount = (TextView) convertView.findViewById(R.id.shopping_count);
            shoppingcount.setText(cart.getSelectCount()+"");
            final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.scGoodSelect);

            checkBox.setChecked(cart.ischecked());

            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    String count = shoppingcount.getText().toString();
                    cart.setSelectCount(Integer.parseInt(count));
                    cart.setChecked(isChecked);
                    selectChangeListenner.calculateCost();
                }
            });
            View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int counts = cart.getSelectCount();
                    switch (v.getId()) {
                        case R.id.shopping_add:
                            counts++;
                            cart.setSelectCount(counts);//数据改变
                            shoppingcount.setText(counts+"");//视图改变
                            if (cart.ischecked()) {
                                selectChangeListenner.calculateCost();
                            }
                            break;
                        case R.id.shopping_lv_reduce:
                            if (counts > 0) {
                                counts--;
                                cart.setSelectCount(counts);
                                shoppingcount.setText(counts+"");//视图改变
                                if (cart.ischecked()) {
                                    selectChangeListenner.calculateCost();
                                }
                            }
                            break;
                    }
                    shoppingcount.setText(counts + "");

                }
            };
            add.setOnClickListener(listener);
            reduce.setOnClickListener(listener);

            return convertView;
        }

    }

    public void setOnselectChangeListenner(OnselectChangeListenner selectChangeListenner) {
        this.selectChangeListenner = selectChangeListenner;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public interface OnselectChangeListenner {
        void calculateCost();
        void groupChecked(int groupId,boolean ischeck);
    }

   /* @Override
    public int getChildTypeCount() {
        return 2;
    }

    @Override
    public int getChildType(int groupPosition, int childPosition) {
        return super.getChildType(groupPosition, childPosition);
    }*/

}
